<?php

namespace app\modules\adminapi\controllers;

use app\models\AttrName;
use app\models\AttrValue;
use app\models\Product;
use app\models\Sku;
use yii\db\ArrayExpression;
use yii\db\JsonExpression;
use yii\db\mysql\JsonExpressionBuilder;

class SkuController extends BaseController
{

    /**
     * @return array
     */
    public function actionCompute()
    {
        $product_id = \Yii::$app->request->post('product_id');
        $product = Product::findOne($product_id);
        $attr_value_ids = \Yii::$app->request->post('attr_value_ids');
        /** @var  $attrValues AttrValue[] */
        $attrValues = !empty($attr_value_ids) ?
            AttrValue::find()->where(['in', 'id', $attr_value_ids])->orderBy('attr_name_id')->all()
            : [];
        $headers = [];
        $desserts = [];
        foreach ($attrValues as $attrValue) {
            /** @var  $attrName AttrName */
            $attrName = $attrValue->attrName;
            $headers[$attrName->id] = ['text' => $attrName->name, 'value' => 'label_' . $attrName->id];
            $desserts[$attrName->id][] = ['label_' . $attrName->id => $attrValue->name, 'id_' . $attrName->id => $attrValue->id];
        }

        $headers['price'] = ['text' => '价格', 'value' => 'price'];
        $headers['stock_num'] = ['text' => '库存', 'value' => 'stock_num'];
        $headers['sale_num'] = ['text' => '销量', 'value' => 'sale_num'];
        $headers['actions'] = ['text' => '操作', 'value' => 'actions'];

        $desserts['price'][] = ['price' => $product ? $product->price : 0];
        $desserts['stock_num'][] = ['stock_num' => 0];
        $desserts['sale_num'][] = ['sale_num' => 0];

        $headers = array_values($headers);
        $desserts = $this->dikaer($desserts);
        foreach ($desserts as &$dessert) {
            $model = Sku::find()->where([
                'and',
                ['JSON EQ', 'attr_data', $dessert['attr_data']],
                ['=', 'product_id', $product_id]
            ])->one();

            if ($model) {
                $dessert['price'] = $model->price;
                $dessert['stock_num'] = $model->stock_num;
                $dessert['sale_num'] = $model->sale_num;
            }
        }


        return [
            'headers' => $headers,
            'desserts' => $desserts,
        ];
    }

    function dikaer($arr)
    {
        $count = 0;
        $result = [];
        foreach ($arr as $key => $val) {
            $arr1 = $result;
            $result = [];
            if ($count == 0) {
                foreach ($val as $v1) {
                    foreach ($v1 as $k => $v) {
                        $v1['attr_data'] = strpos($k, 'id_') !== false ? [str_replace('id_', '', $k) => $v] : [];
                    }

                    $result[] = $v1;
                }
            } else {
                foreach ($arr1 as $v1) {
                    foreach ($val as $v2) {
                        foreach ($v2 as $k => $v) {
                            if (strpos($k, 'id_') !== false) {
                                $v1['attr_data'][str_replace('id_', '', $k)] = $v;
                            }
                        }
                        $result[] = $v1 + $v2;
                    }
                }
            }
            $count++;
        }
//        $result = array_shift($arr);
//        while ($arr2 = array_shift($arr)) {
//            $arr1 = $result;
//            $result = array();
//            foreach ($arr1 as $k1 => $v1) {
//                foreach ($arr2 as $k2 => $v2) {
//                    if (!is_array($v1)) $v1 = array($k1 => $v1);
//                    if (!is_array($v2)) $v2 = array($k2 => $v2);
//                    $result[] = array_merge_recursive($v1, $v2);
//                }
//            }
//        }
        return $result;
    }
}
